# -*- coding: utf-8 -*-

from django.db import models
from django.db.models.signals import post_save

from django.contrib.auth.models import User

from django.core.exceptions import ValidationError

class ProtectedModel(models.Model):
  class Meta:
    abstract = True
  
  def check_deps(self):
    return True
  
  def delete(self, *args, **kwargs):
    if self.check_deps():
      super(ProtectedModel, self).delete(*args, **kwargs)
    else:
      raise Exception(u'Predmet je uporabljen, in se ga ne sme izbrisati')

class Profile(models.Model):
  user = models.OneToOneField(User, related_name = '+')
  professor = models.OneToOneField('exams.Professor', blank = True, null = True, related_name = '+')
  student = models.OneToOneField('student.Student', blank = True, null = True, related_name = '+')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')
  
  def save(self, *args, **kwargs):
    
    # Perhaps there is no need for overloading save method. But since it's called
    # when syncdb is executed, it's quite convinient
    try:
      self.student = self.user.student
    except:
      try:
        self.professor = self.user.professor
      except:
        pass
    
    super(Profile, self).save(*args, **kwargs)

class Commune(ProtectedModel):
  code = models.CharField(primary_key = True, max_length = 3, verbose_name = u'Šifra')
  name = models.CharField(max_length = 200, verbose_name = 'Ime')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')
  
  class Meta:
    ordering = ['code']
    verbose_name = u'Občina'
    verbose_name_plural = u'Občine'
  
  def __unicode__(self):
    return self.name
  
  def check_deps(self):
    if self.professor_set.count() + self.permanent_enrollment_sheet_set.count() + self.temporary_enrollment_sheet_set.count() > 0:
      return False
    return True

class Country(ProtectedModel):
  code = models.CharField(primary_key = True, max_length = 3, verbose_name = u'Šifra')
  name = models.CharField(max_length = 200, db_index = True, verbose_name = 'Ime')
  international_name = models.CharField(max_length = 200, verbose_name = 'Mednarodno ime')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')
  
  class Meta:
    ordering = ['name']
    verbose_name = u'Država'
    verbose_name_plural = u'Države'
  
  def __unicode__(self):
    return self.name
  
  def check_deps(self):
    if self.enrollmentsheet_set.count() > 0:
      return False
    return True

class Post(ProtectedModel):
  code = models.CharField(primary_key = True, max_length = 4, verbose_name = u'Šifra')
  name = models.CharField(max_length = 200, db_index = True, verbose_name = 'Ime')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')
  
  class Meta:
    ordering = ['code']
    verbose_name = u'Pošta'
    verbose_name_plural = u'Pošte'
  
  def __unicode__(self):
    return u'%s %s' % (self.code, self.name)
  
  def check_deps(self):
    if self.professor_set.count() + self.temporary_enrollment_sheet_set.count() + self.permanent_enrollment_sheet_set.count() > 0:
      return False
    return True

class Program(ProtectedModel):
  code = models.CharField(max_length = 4, primary_key = True, verbose_name = u'Šifra')
  name = models.CharField(max_length = 200, verbose_name = 'Ime')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')
  subjects = models.ManyToManyField('exams.Subject', through = 'exams.SubjectProgram', related_name = 'programs')
  modules = models.ManyToManyField('ProgramModule', related_name = 'programs', verbose_name = "Moduli")
  
  class Meta:
    verbose_name = u'Študijski program'
    verbose_name_plural = u'Študijski programi'
    ordering = ['name']
  
  def __unicode__(self):
    return self.name

  def check_deps(self):
    if self.enrollmentsheet_set.count() + self.subject_set.count() > 0:
      return False
    return True

class ProgramModule(ProtectedModel):
  id = models.AutoField(primary_key=True)
  name = models.CharField(max_length = 200, verbose_name = 'Ime')
  year = models.IntegerField(verbose_name = 'Letnik')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')

  class Meta:
    verbose_name = u'Študijski modul'
    verbose_name_plural = u'Študijski moduli'

  def __unicode__(self):
    return self.name
  
  def check_deps(self):
    if self.studentmodule_set.count() + self.subject_set.count() > 0:
      return False
    return True

class CourseGroup(ProtectedModel):
  code = models.CharField(primary_key = True, max_length = 4, verbose_name = u'Šifra')
  name = models.CharField(max_length = 200, verbose_name = 'Ime')
  inactive = models.BooleanField(default = False, verbose_name = 'Neveljaven')
  
  class Meta:
    verbose_name = u'Študijska smer'
    verbose_name_plural = u'Študijske smeri'
  
  def __unicode__(self):
    return self.name
  
  def check_deps(self):
    if self.enrollmentsheet_set.count() + self.subject_set.count() > 0:
      return False
    return True

# Some callbacks
def create_profile_callback(instance, created, **kwargs):
  if created is True:
    # Create the profile and connect it
    profile = Profile()
    profile.user = instance
    profile.save()

# Connect with some signals
post_save.connect(create_profile_callback, sender = User)
